/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.component.controls;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.StringProperty;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * 自定义密码框
 * @author zpy
 * @version 1.0.0
 */
public class PesPasswordField extends VBox {
    private final Label requiredTag = new Label("*");
    private final Label textLabel = new Label();
    private final PasswordField passwordField = new PasswordField();
    private final Label textError = new Label();
    private final BooleanProperty required = new SimpleBooleanProperty(false);
    private PesPasswordField firstPasswordField;

    public PesPasswordField(){
        HBox labelBox = new HBox();
        labelBox.setAlignment(Pos.CENTER_LEFT);
        labelBox.getChildren().addAll(requiredTag,this.textLabel);
        this.requiredTag.managedProperty().bind(this.requiredTag.visibleProperty());
        this.requiredTag.getStyleClass().add("pes-text-label-req");
        this.textLabel.getStyleClass().add("pes-text-label");
        this.passwordField.getStyleClass().add("pes-text-field");
        this.textError.getStyleClass().add("pes-text-error");
        this.getChildren().setAll(labelBox,passwordField,textError);
        this.passwordField.focusedProperty().addListener((o, oldVal, newVal)->{
            if(!newVal){
                validContent();
            }
        });
        this.required.addListener((observableValue, aBoolean, t1) -> {
            if(Objects.nonNull(t1)){
                this.requiredTag.setVisible(t1);
            }
        });
        this.setRequired(false);
        this.requiredTag.setVisible(false);
        this.firstPasswordField = null;
    }

    public void setLabel(String label){
        this.textLabel.setText(label);
    }

    public String getLabel(){
        return this.textLabel.getText();
    }

    public void setRequired(boolean req){
        this.required.setValue(req);
    }

    public boolean isRequired(){
        return this.required.get();
    }

    public boolean validate(){
        return validContent();
    }

    public void setText(String value){
        this.passwordField.setText(value);
    }

    public String getText(){
        return this.passwordField.getText();
    }

    public void setFieldDisable(boolean value){
        this.passwordField.setDisable(value);
    }

    public StringProperty textProperty(){
        return this.passwordField.textProperty();
    }

    public void resetValidation(){
        this.textError.setText("");
    }

    public void setFirstPasswordField(PesPasswordField field){
        this.firstPasswordField = field;
    }

    private boolean validContent(){
        if(!this.isRequired()){
            return true;
        }
        if(StringUtils.isBlank(this.passwordField.getText())){
            this.textError.setText(textLabel.getText().concat("不能为空"));
            return false;
        }else{
            this.textError.setText("");
        }
        if(Objects.nonNull(this.firstPasswordField)){
            if(!StringUtils.equals(this.getText(),this.firstPasswordField.getText())){
                this.textError.setText(String.format("%s不一致",this.getLabel()));
                return false;
            }
        }
        return true;
    }
}